package com.cykj.enterprise.service.impl;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.CompanyDO;
import com.cykj.common.entity.OrderDO;
import com.cykj.common.entity.VipDO;
import com.cykj.common.utils.SnowflakeUtil;
import com.cykj.enterprise.mapper.HrMapper;
import com.cykj.enterprise.mapper.PayMapper;
import com.cykj.enterprise.service.IPayService;
import com.cykj.enterprise.template.AlipayTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.enterprise.service.impl
 * @Author: 蔡张君逸
 * @CreateTime: 2025-08-30 00:00
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class PayServiceImpl implements IPayService {
    static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Autowired
    PayMapper payMapper;
    @Autowired
    SnowflakeUtil snowflakeUtil;
    @Autowired
    HrMapper hrMapper;
    @Resource
    AlipayTemplate alipayTemplate;
    @Override
    public String pay(int hrId, int num, int duration) {
        OrderDO order = new OrderDO();
        order.setOrderNumber(String.valueOf(snowflakeUtil.nextId()));
        order.setAmount((double) num);
        order.setRechargeRole(1);
        order.setRelativeId(hrId);
        order.setBuyDuration(duration);


        try {
            return alipayTemplate.pay(order);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseDTO getVipInfo(String hrId) {
        CompanyDO companyDO=payMapper.getVipInfo(hrId);
        if (companyDO!=null){
        return ResponseDTO.success(companyDO);
    }
        return ResponseDTO.error("未通过认证");
    }

    @Override
    public ResponseDTO getVipGoods() {
        List<VipDO> vipDOS =payMapper.getVipGoods(1);
        return ResponseDTO.success(vipDOS);
    }

    @Override
    public ResponseDTO updateVipTime(Integer relativeId,Integer duration) {
        CompanyDO vipInfo = payMapper.getVipInfo(String.valueOf(relativeId));
        LocalDateTime localDateTime;
        if (LocalDateTime.parse(vipInfo.getVipEndTime(),formatter).compareTo(LocalDateTime.now())>0) {
//            会员还没过期，延长时间
            localDateTime = LocalDateTime.parse(vipInfo.getVipEndTime(), formatter).plusMonths(duration);
            int res=payMapper.updateVipTime(vipInfo.getCompanyId(),localDateTime);


        }else {
            //会员过期，从现在时间再加时间
            localDateTime = LocalDateTime.now().plusMonths(duration);
            int res=payMapper.updateVipTime(vipInfo.getCompanyId(),localDateTime);
        }
        return null;
    }
}